/**
 * 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
 */

class WxgamePlatform {

  name = 'wxgame';
  openid = "abc";
  login() {
   
    return new Promise((resolve, reject) => {
      wx.login({
        success: (res) => {
          // resolve(res)
          if (res.code) {
            resolve(res.code);
            console.log(res.code);
          }
        }
      })
    })
  }

  getUserInfo() {
    return new Promise((resolve, reject) => {
      wx.getUserInfo({
        withCredentials: true,
        success: function(res) {
          var userInfo = res.userInfo
          var nickName = userInfo.nickName
          var avatarUrl = userInfo.avatarUrl
          var gender = userInfo.gender //性别 0：未知、1：男、2：女
          var province = userInfo.province
          var city = userInfo.city
          var country = userInfo.country
          resolve(userInfo);
        }
      })
    })
  }
  //分享
  showShareMenu() {
    return new Promise((resolve, rejext) => {
      wx.showShareMenu({
        withShareTicket:true,
        success:res => {
          console.log("showShareMenu Success");
          this.onshareMessage();
          resolve(res);
        },
        fail:res => {
          console.log("fail"+res.errMsg);
        }
      })
      
      

    })
  }
  //分享内容
  onshareMessage(){
    wx.onShareAppMessage(()=> {
      return {
        title: '飞机对战',
        imageUrl:"openDataContext/assets/logo.png",
        //query:"key1="+this.openid
      }
    })
  }

  /**自制微信分享+内容 */
  activeShare(userid) {
    return new Promise((resolve, reject) => {
      var data = null;
      wx.shareAppMessage({
        title: "好友对战",
        imageUrl: "openDataContext/assets/logo.png",
        query: "key1=" + userid,
        success: function (res) {
          console.log("转发成功" + JSON.stringify(res));
          data = res;
        },
        fail: function (res) {
          console.log("转发失败" + res.errMsg);
          data = res;
        },
        complete: function () {
          console.log("转发接口调用完成");
          resolve(data);
        }
      })
    })
  }



  //读取启动参数
  getLaunchOptionsSync(){
    return new Promise((resolve, rejext) =>{
      var option =  wx.getLaunchOptionsSync();
      resolve(option);
      console.log("微信读取登录信息");
      console.log(option.query);
    })

  }
  

  openDataContext = new WxgameOpenDataContext();
}

class WxgameOpenDataContext {

  createDisplayObject(type, width, height) {
    const bitmapdata = new egret.BitmapData(sharedCanvas);
    bitmapdata.$deleteSource = false;
    const texture = new egret.Texture();
    texture._setBitmapData(bitmapdata);
    const bitmap = new egret.Bitmap(texture);
    bitmap.width = width;
    bitmap.height = height;

    if (egret.Capabilities.renderMode == "webgl") {
      const renderContext = egret.wxgame.WebGLRenderContext.getInstance();
      const context = renderContext.context;
      ////需要用到最新的微信版本
      ////调用其接口WebGLRenderingContext.wxBindCanvasTexture(number texture, Canvas canvas)
      ////如果没有该接口，会进行如下处理，保证画面渲染正确，但会占用内存。
      if (!context.wxBindCanvasTexture) {
        egret.startTick((timeStarmp) => {
          egret.WebGLUtils.deleteWebGLTexture(bitmapdata.webGLTexture);
          bitmapdata.webGLTexture = null;
          return false;
        }, this);
      }
    }
    return bitmap;
  }
  postMessage(data) {
    const openDataContext = wx.getOpenDataContext();
    openDataContext.postMessage(data);
  }




}


window.platform = new WxgamePlatform();